# Maintainer: Renato Silva <br.renatosilva@gmail.com>

_realname='nsis'
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=3.09
pkgrel=1
url='https://nsis.sourceforge.io/'
pkgdesc='Windows installer development tool (mingw-w64)'
license=('spdx:Zlib')
arch=(any)
mingw_arch=('mingw32' 'mingw64' 'ucrt64')
depends=(${MINGW_PACKAGE_PREFIX}-zlib
         ${MINGW_PACKAGE_PREFIX}-gcc-libs
         ${MINGW_PACKAGE_PREFIX}-libwinpthread)
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             'dos2unix'
             'scons>=2.3.4-3'
             rsync)
source=("https://sourceforge.net/projects/nsis/files/NSIS%203/${pkgver}/${_realname}-${pkgver}-src.tar.bz2"
        001-fhs-directory-structure.patch
        002-compile-fixes.patch
        003-remove-redundant-architecture-suffix.patch
        005-fix-call-to-non-constexpr-function.patch
        006-force-c++11.patch)
sha256sums=('0cd846c6e9c59068020a87bfca556d4c630f2c5d554c1098024425242ddc56e2'
            'a2719915676106bc0c545fca6d5871c96044c7e4ba8c95ea5ca03a0a49b2b6e3'
            '4b94e5f5979ea13e19a37606ec5861dfdc0004b9e4eaf984e4aae4a7fe5e85b7'
            'f43af517d7821084335a38bce15c7644cc114c71cf6f8ab860aa4d9322795343'
            'ff3e9afb2970eb33ff6963f166c77ee2e3b40151f4dc9350968c428c85d725d0'
            'caadcf0a728aa16b55fd8c0f3e65100a6df711e1b30e35d80c0dceaef4e2a455')

# Circumvent problem where makepkg will add the exe extension to some files
# when compressing the 64-bit package
options=('!strip')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

_consolidate() {
  msg2 'Converting to unix line breaks'
  find -name 'SConstruct' | xargs dos2unix --quiet
  find -name 'SConscript' | xargs dos2unix --quiet
  find -name '*.[ch]'     | xargs dos2unix --quiet
  find -name '*.cpp'      | xargs dos2unix --quiet
  find -name '*.py'       | xargs dos2unix --quiet
}

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}-src"
  _consolidate

  _apply_patch_with_msg \
    001-fhs-directory-structure.patch \
    002-compile-fixes.patch \
    003-remove-redundant-architecture-suffix.patch \
    005-fix-call-to-non-constexpr-function.patch \
    006-force-c++11.patch
}

_build() {
  local target_arch="${CARCH/i686/x86}"
  target_arch="${target_arch/x86_64/amd64}"
  
  local arrVer=(${pkgver//./ })
  local arrLen=${#arrVer[@]}
  local major_version=${arrVer[0]}
  local minor_version=${arrVer[1]}
  local revision_version='0'
  local build_version='0'
  if ((arrLen > 2)); then
    revision_version=${arrVer[2]}
    if ((arrLen > 3)); then
      build_version=${arrVer[3]}
    fi
  fi

  minor_version="${minor_version%%[a-z]*}"
  msg2 "Version parts ${arrLen}"
  msg2 "Major version defined as ${major_version}"
  msg2 "Minor version defined as ${minor_version}"
  msg2 "Revision version defined as ${revision_version}"
  msg2 "Build version defined as ${build_version}"

  cd "${srcdir}/build-${CARCH}-${_realname}-${pkgver}"
  /usr/bin/scons \
      TARGET_ARCH="${target_arch}" \
      PREFIX="${MINGW_PREFIX}" \
      VERSION="${pkgver}" \
      VER_MAJOR="${major_version}" \
      VER_MINOR="${minor_version}" \
      VER_REVISION="${revision_version}" \
      VER_BUILD="${build_version}" \
      UNICODE='yes' \
      NSIS_MAX_STRLEN='8192' \
      NSIS_CONFIG_CONST_DATA_PATH='no' \
      SKIPUTILS='NSIS Menu,SubStart' \
      PREFIX_DEST="${PREFIX_DEST}" \
      NSIS_CONFIG_LOG='yes' \
      ${target}
}

build() {
  msg2 'Synchronizing build directory'
  rsync --recursive --times "${srcdir}/${_realname}-${pkgver}-src"/* "${srcdir}/build-${CARCH}-${_realname}-${pkgver}"
  _build
}

package() {
  PREFIX_DEST="${pkgdir}" target='install' _build
  install -Dm644 "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
  rm "${pkgdir}${MINGW_PREFIX}/share/doc/nsis/COPYING"
}
